package tongwentongshu.com.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.ext.sns.TIMFriendshipManagerExt;

import java.util.List;

import tongwentongshu.com.app.R;
import tongwentongshu.com.app.activityim.ChatActivity;


/**
 * 类描述：
 * 创建人： 史强
 * 创建时间:2017/7/24 13:43
 */
public class ContactFragment extends Fragment {

    private View view;
    private ListView lv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (view == null) {
            view = inflater.inflate(R.layout.fragment_contact, container, false);
            lv = (ListView) view.findViewById(R.id.lv_friends);

        }
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        //获取好友列表
        TIMFriendshipManagerExt.getInstance().getFriendList(new TIMValueCallBack<List<TIMUserProfile>>() {
            @Override
            public void onError(int code, String desc) {
                //错误码code和错误描述desc，可用于定位请求失败原因
                //错误码code列表请参见错误码表
                Log.e("shiqiang", "getFriendList failed: " + code + " desc");
            }

            @Override
            public void onSuccess(final List<TIMUserProfile> result) {

                MyAdapter adapter = new MyAdapter(result);
                final String name = result.get(0).getNickName().isEmpty() ? result.get(0).getIdentifier(): result.get(0).getNickName();

                final String imageUrl =result.get(0).getFaceUrl().isEmpty() ? "" : result.get(0).getFaceUrl();
                lv.setAdapter(adapter);

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                        ChatActivity.navToChat(getActivity(), result.get(i).getIdentifier(), TIMConversationType.C2C , imageUrl ,name);

                    }
                });

                for (TIMUserProfile res : result) {
                    Log.e("dd", "identifier: " + res.getIdentifier() + " nickName: " + res.getNickName()
                            + " remark: " + res.getRemark() + "imageURL:" + res.getFaceUrl());
                }
            }
        });


    }

    private class MyAdapter extends BaseAdapter {

        private List<TIMUserProfile> result;
        private View convertView;

        public MyAdapter(List<TIMUserProfile> result) {

            this.result = result;


        }

        @Override
        public int getCount() {
            return result.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        /*书中详细解释该方法*/
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            //观察convertView随ListView滚动情况
            if (convertView == null) {
                convertView = parent.inflate(getActivity(), R.layout.item,
                        null);
                holder = new ViewHolder();
                    /*得到各个控件的对象*/
                holder.title = (TextView) convertView.findViewById(R.id.name);
                convertView.setTag(holder);//绑定ViewHolder对象
            } else {
                holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
            }
            /*设置TextView显示的内容，即我们存放在动态数组中的数据*/
            holder.title.setText(result.get(position).getNickName() + result.get(position).getIdentifier());


            return convertView;
        }

    }

    /*存放控件*/
    public final class ViewHolder {
        public TextView title;
        public TextView text;
        public Button bt;
    }
}
